package org.acghub.config.type;

import org.acghub.config.DataVerifyException;
import org.acghub.config.Type;
import org.acghub.config.TypeInstanceException;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/24/星期四 14:08
 * @Version 1.0
 */
public class FloatType implements Type.SimpleType<Float> {
    @Override
    public String name() {
        return "float";
    }

    @Override
    public String javaInterface() {
        return "Float";
    }

    @Override
    public String defaultValue() {
        return ".0F";
    }

    @Override
    public String exportVerify(String value, String delimiters) throws DataVerifyException {
        try {
            double v = Double.parseDouble(value);
            float fv = (float) v;
            if (v != fv) {
                throw new DataVerifyException("verify float value:" + value);
            }
            return Float.toString(fv);
        } catch (DataVerifyException dve) {
            throw dve;
        } catch (Exception e) {
            throw new DataVerifyException("verify float value:" + value, e);
        }
    }

    @Override
    public Float instance(String value, String delimiters) throws TypeInstanceException {
        try {
            return Float.parseFloat(value);
        } catch (NumberFormatException nfe) {
            throw new TypeInstanceException("parse float value:" + value, nfe);
        }
    }

}
